<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户登录 - Cloud DMS</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
    <script src="https://cdn.jsdelivr.net/npm/layui-src/dist/layui.js"></script>
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Arial', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            width: 420px;
            padding: 40px;
            background-color: rgba(255, 255, 255, 0.95);
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.1);
            backdrop-filter: blur(10px);
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-title {
            font-size: 28px;
            font-weight: bold;
            color: #4A90E2;
            margin-bottom: 8px;
        }
        .form-item {
            margin-bottom: 20px;
        }
        .btn-login {
            width: 100%;
            height: 44px;
            background: linear-gradient(135deg, #4A90E2 0%, #667eea 100%);
            border: none;
            color: white;
            font-size: 16px;
            font-weight: bold;
            margin-top: 10px;
        }
        .btn-login:hover {
            opacity: 0.9;
        }
        .form-links {
            text-align: center;
            margin-top: 20px;
        }
        .form-links a {
            color: #4A90E2;
            text-decoration: none;
            margin: 0 10px;
        }
        .form-links a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <div class="login-title">Cloud DMS</div>
            <p style="color: #666; margin: 0;">云存储数据管理系统</p>
        </div>
        
        <form class="layui-form" id="loginForm">
            <div class="form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" placeholder="用户名" 
                           lay-verify="required" lay-reqtext="用户名不能为空" 
                           class="layui-input" autocomplete="off">
                </div>
            </div>
            
            <div class="form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" placeholder="密码" 
                           lay-verify="required" lay-reqtext="密码不能为空" 
                           class="layui-input" autocomplete="off">
                </div>
            </div>
            
            <div class="form-item">
                <input type="checkbox" name="remember" title="记住我" lay-skin="primary">
            </div>
            
            <button type="submit" class="layui-btn btn-login" lay-submit lay-filter="login">
                登录
            </button>
        </form>
        
        <div class="form-links">
            <a href="/auth/register">注册新账户</a>
            <a href="/auth/admin-login">管理员登录</a>
        </div>
    </div>

    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            
            // 检查是否已登录
            checkLoginStatus();
            
            // 监听登录提交
            form.on('submit(login)', function(data) {
                var loadIndex = layer.load(1, {
                    content: '登录中...',
                    time: 10000
                });
                
                // 构建登录请求
                var loginData = {
                    username: data.field.username,
                    password: data.field.password,
                    userType: 'USER'
                };
                
                // 发送登录请求
                fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(loginData)
                })
                .then(response => response.json())
                .then(result => {
                    layer.close(loadIndex);
                    
                    if (result.success) {
                        // 保存Token
                        localStorage.setItem('token', result.token);
                        localStorage.setItem('username', result.username);
                        localStorage.setItem('userId', result.userId);
                        localStorage.setItem('roles', JSON.stringify(result.roles));
                        
                        layer.msg('登录成功！', {icon: 1}, function() {
                            // 跳转到用户控制台
                            window.location.href = '/auth/user-console';
                        });
                    } else {
                        layer.msg(result.message || '登录失败', {icon: 2});
                    }
                })
                .catch(error => {
                    layer.close(loadIndex);
                    console.error('登录请求失败:', error);
                    layer.msg('网络错误，请稍后重试', {icon: 2});
                });
                
                return false; // 阻止表单默认提交
            });
            
            // 检查登录状态
            function checkLoginStatus() {
                var token = localStorage.getItem('token');
                if (token) {
                    // 验证Token是否有效
                    fetch('/api/auth/status', {
                        headers: {
                            'Authorization': 'Bearer ' + token
                        }
                    })
                    .then(response => response.json())
                    .then(result => {
                        if (result.authenticated) {
                            // 已登录，跳转到控制台
                            window.location.href = '/auth/user-console';
                        }
                    })
                    .catch(error => {
                        // Token无效，清除本地存储
                        localStorage.clear();
                    });
                }
            }
        });
    </script>
</body>
</html>